<?php
/**
 * Create By: PhpStorm
 * User: yihua
 * File: Useraddress.php
 * Date: 2016/11/27
 * Time: 23:48
 */

namespace app\api\controller;

use app\api\model\page\user\AddUserAddress as AddUserAddressModel;
use app\api\model\page\user\GetUserAddress as GetUserAddressModel;
use app\api\model\page\user\UpdateUserAddress as UpdateUserAddressModel;
use app\api\model\page\user\RemoveUserAddress as RemoveUserAddressModel;
use app\api\model\page\user\SetDefaultAddress as SetDefaultAddressModel;
use think\Request;
use app\library\aliyun\oss\OssWorker;
use app\library\aliyun\sts\StsWorker;

class UserAddress
{
    private $param;

    public function __construct()
    {
        $this->param = Request::instance()->param();
    }

    public function getuserAddress()
    {
        /*$objSts = new StsWorker(1);
        $sts = $objSts->getToken();
        return $sts;
        */


        //$obj = new OssWorker();
        //return $obj->uploadFile('test',123,'test');
        $userAddressModel = new GetUserAddressModel();
        return $userAddressModel->excute($this->param);
    }

    public function addUserAddress()
    {
        $userAddressModel = new AddUserAddressModel();
        return $userAddressModel->excute($this->param);
    }

    public function removeUserAddress()
    {
        $userAddressModel = new RemoveUserAddressModel();
        return $userAddressModel->excute($this->param);
    }

    public function updateUserAddress()
    {
        $userAddressModel = new UpdateUserAddressModel();
        return $userAddressModel->excute($this->param);
    }

    public function setDefaultAddress()
    {
        $userAddressModel = new SetDefaultAddressModel();
        return $userAddressModel->excute($this->param);
    }
    /*

       public function removeUserAddress()
       {
       if (!isset($this->param['user_id']) || !isset($this->param['address_id'])) {
       return $this->getRes(Error::ERR_PARAM);
       }
       $res = $this->userAddressModel->removeAddress($this->param['user_id'], $this->param['address_id']);
       if ($res != 0) {
       return $this->getRes();
       } else {
       $this->data = $res;
       return $this->getRes(Error::ERR_USER_ADDRESS_REMOVE);
       }
       }


       public function updateUseraddress()
       {
       if (!isset($this->param['address_id'])) {
       return $this->getRes(Error::ERR_PARAM);
       }
       $res = $this->userAddressModel->updateAddress($this->param);
       if (false === $res) {
       return $this->getRes(Error::ERR_USER_ADDRES_UPDATE);
       } else {
       return $this->getRes();
       }
       }


       public function getUserAddress()
       {
    //return var_dump(Flag::SMS_EXPIRE_TIME);
    //return Common::createVerificationCode(18515030719,6,1);


    //默认查询有效的地址
    //$this->data = Db::table($this->table_name)->where('status',1)->field('create_time,update_time',true)->select();
    if (!isset($this->param['user_id'])) {
    return $this->getRes(Error::ERR_PARAM);
    }
    $res = $this->userAddressModel->getAddressByUserid($this->param['user_id']);
    //var_dump($res);
    if (false === $res) {
    return $this->getRes(Error::ERR_SYS);
    } else {
    $this->data = $res;
    return $this->getRes();
    }
    }

    public function setDefaultAddress()
    {
    if (!isset($this->param['address_id']) || !isset($this->param['user_id'])) {
    Log::alert(__METHOD__ . ' param: ' . json_encode($this->param));
    return $this->getRes(Error::ERR_PARAM);
    }
    $ret = $this->userAddressModel->setDefaultAddress($this->param['address_id'], $this->param['user_id']);
    if (false === $ret) {
    return $this->getRes(Error::ERR_SYS);
    }
    return $this->getRes();
    }


    public function check($param)
    {
    $rule = [
    'address_name' => 'require|max:25',
    'user_id' => 'require',
    'consignee' => 'require',
        'address' => 'require',
        'mobile' => 'require',
        'province' => 'require',
        'city' => 'require',
        'district' => 'require',
        ];
    $msg = [
        'address_name.require' => '名称必须',
        'address_name.max' => '名称最多不能超过25个字符',
        'user_id.require' => 'userid必须',
        'consignee.require' => 'consignee必须',
        'address.require' => 'address必须',
        'mobile.require' => 'mobile必须',
        'province.require' => 'province必须',
        'city.require' => 'city必须',
        'district.require' => 'district必须',
        ];
    $data = [
        'name' => 'thinkphp',
        'age' => 121,
        'email' => 'thinkphp@qq.com',
        ];
    $validate = new Validate($rule, $msg);
    $result = $validate->check($param);
    if (!$result) {
        return $validate->getError();
    }
    return $result;
}
*/
}

